package com.bwie.auth.controller;

import com.alibaba.fastjson.JSON;
import com.bwie.auth.service.AuthService;
import com.bwie.common.domain.User;
import com.bwie.common.domain.response.Jwtresponse;
import com.bwie.common.result.Result;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/Auth")
@Log4j2
public class AuthController {
    @Autowired
    AuthService authService;

    @Autowired
    HttpServletRequest request;

    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Result<Jwtresponse> login(@RequestBody User user){
       log.info("功能名称，登录，请求URI：{}，请求方式：{}，请求参数：{}",
               request.getRequestURI(),request.getMethod(),user);

       Result<Jwtresponse> result=authService.login(user);

       log.info("功能名称，登录，请求URI：{}，请求方式：{}，响应结果：{}",
               request.getRequestURI(),request.getMethod(), JSON.toJSONString(result));
       return result;
    }

    @GetMapping("/info")
    public Result<User> info(){
        log.info("功能名称，获取用户信息，请求URI：{}，请求方式：{}",
                request.getRequestURI(),request.getMethod());

        User user=authService.info();

        log.info("功能名称，获取用户信息，请求URI：{}，请求方式：{}，响应结果：{}",
                request.getRequestURI(),request.getMethod(),JSON.toJSONString(user));
        return Result.success(user);
    }
}
